package com.tanhua.app.controller;

import com.tanhua.app.service.UserService;
import com.tanhua.domain.UserHolder;
import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class UserInfoController {

    @Autowired
    private UserService userService;


    @GetMapping(value = "/users", name = "登录用户的数据回显")
    public ResponseEntity findUser(Long userID,Long huanxinID){
        if (userID != null) { //userID查询
            return userService.findUserInfoVo(userID);
        } else if (huanxinID != null) { // 2、根据huanxinID查询
            return userService.findUserInfoVo(huanxinID);
        }else{ //根据登录用户id查询
            User user = UserHolder.findUser();
            return userService.findUserInfoVo(user.getId());
        }
    }


    @PostMapping(value = "/users/header", name = "登录用户的数据修改/头像")
    public ResponseEntity updateHeader(MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws IOException {

        return userService.loginReginfoimg(headPhoto,token);
    }


    @PutMapping(value = "/users", name = "登录用户的数据修改/数据")
    public ResponseEntity updateInfo(@RequestHeader("Authorization") String token, @RequestBody UserInfo userInfo) throws IOException {
        return userService.updateInfo(token,userInfo);
    }



    @GetMapping(value = "/users/settings", name = "陌生人问题和通知设置的查询")
    public ResponseEntity userSettings() throws IOException {
        return userService.userSettings();
    }

    @PostMapping(value = "/users/questions", name = "新增/修改陌生人问题")
    public ResponseEntity saveOrUpdateQuestions(@RequestBody Map param) throws IOException {
        String content =(String)param.get("content");

        return userService.saveOrUpdateQuestions(content,UserHolder.findUser().getId());
    }


    @PostMapping(value = "/users/notifications/setting", name = "新增/修改通知")
    public ResponseEntity saveOrUpdateSettings(@RequestBody Settings settings) throws IOException {

        return userService.saveOrUpdateSettings(settings,UserHolder.findUser().getId());
    }



    @GetMapping(value = "/users/blacklist", name = "分页查询登录用户黑名单")
    public ResponseEntity findBlacklist(Integer page,Integer pagesize) throws IOException {
        return userService.findBlacklist(page,pagesize,UserHolder.findUser().getId());
    }


    @DeleteMapping(value = "/users/blacklist/{uid}", name = "删除黑名单用户")
    public ResponseEntity deleteBlack(@PathVariable("uid") Long uid) throws IOException {
        return userService.deleteBlack(uid,UserHolder.findUser().getId());
    }


    @GetMapping(value = "/users/counts", name = "统计粉丝 喜欢 互相喜欢数量")
    public ResponseEntity findCounts() throws IOException {
        return userService.findCounts();
    }

    @GetMapping(value = "/users/friends/{type}", name = "分页展示粉丝 喜欢 互相喜欢数据")
    public ResponseEntity findTypePage(@PathVariable("type") Integer type,
                                     @RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10")Integer pagesize) throws IOException {
        return userService.findTypePage(page,pagesize,type);
    }


    @PostMapping(value = "/users/fans/{uid}", name = "粉丝喜欢变成好友关系")
    public ResponseEntity fansToFriend(@PathVariable("uid") Long fansId) throws IOException {
        return userService.fansToFriend(fansId);
    }

    @DeleteMapping(value = "/users/like/{uid}", name = "喜欢的人不在喜欢 互相喜欢的人变成粉丝")
    public ResponseEntity FriendTofans(@PathVariable("uid") Long loveId) throws IOException {
        return userService.FriendTofans(loveId);
    }





}
